import axios from 'axios';
import { Dialog } from 'cube-ui'
import Vue from 'vue'
import {getQuery } from '@/utils/auth'
Vue.use(Dialog)

//http://localhost:8080/customerServicePage?userid=10754&token=9e9c09b6f60948c380aeeec47aa7a3gy&level=1&gv=5.1.0&p=%5Baid%3Aw-4463-74687-7358-93032-78363-72274%5D,%5Bcode%3A0%5D,%5Blan%3Azh%5D,%5Bsvc%3A0%5D,%5Bsvn%3A0%5D,%5Bcvn%3A0%5D,%5Bcvc%3A0%5D,%5Bchn%3Aweb%5D,%5Bpkg%3Acom.full.party%5D

let url = ''
if (process.env.NODE_ENV === 'production'){ //production
    url = 'http://pyy89.com:9292'
    // url = 'http://18.136.192.121:9292'
}else if(process.env.NODE_ENV === 'development'){ //development
    url = 'http://18.136.192.121:9292'
}

const service = axios.create({
    baseURL: url,
    timeout: 400000,
    withCredentials: true
});

// Request interceptors
service.interceptors.request.use(
    (config) => {
        let data=getQuery()
        config.headers['X-AUTH-USER']  = data.userid
        config.headers['X-AUTH-TOKEN'] = data.token
        let ua=navigator.userAgent.substring(navigator.userAgent.indexOf("(")+1,navigator.userAgent.indexOf(")"));
        let p=data.p;
            config.headers['X-AUTH-PROID'] = data.pro_id
        if(config.params){
            config.params.player_id = data.userid
            config.params.pro_id=data.pro_id
            config.params.pkg=p['pkg']
            config.params.chn=p['chn']
        }else if(config.data){
            config.data.player_id = data.userid
            config.data.pro_id= data.pro_id
            config.data.chn = p['chn']
            config.data.game_version =data.gv
            config.data.ua =ua
            config.data.platform = p['svn']
            config.data.level =Number(data.level)
        }
        return config
    },
    (error) => {
        Promise.reject(error);
    },
);
// Response interceptors
service.interceptors.response.use(
    response => {
        /**
         * status为非0是抛错 可结合自己业务进行修改
         */
        return response
    },
    error => {
        if(error.response){
            if(error.response.data.status===2001) {
                Dialog.$create({
                    type: 'alert',
                    content:error.response.data.msg,
                    confirmBtn: {
                        text: '我知道了',
                        active: true
                    }
                }).show()
            }
        }else{
        Object.assign(error,{response:{data:{status:-400}}})
        }

        return Promise.reject(error)
    }
);

export default service;
